﻿using DesktopShortcutMonitor.Monitor;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DesktopShortcutMonitor
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            string productName = Application.ProductName;
            bool createNew;
            using (Mutex mutex = new Mutex(true, productName, out createNew))
            {
                if (createNew)
                {
                    App.Ins.Init();
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                }
                else
                {
                    // 查找并激活已存在的窗口
                    IntPtr hWnd = FindWindow(null, productName);
                    if (hWnd != IntPtr.Zero)
                    {
                        ShowWindow(hWnd, SW_RESTORE); // 恢复窗口
                        SetForegroundWindow(hWnd);    // 激活到前台
                    }
                    UIMessageBox.ShowInfo($"程序{productName}已经启动");
                }

            }

        }

        // 导入Win32 API
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        private const int SW_RESTORE = 9;

    }
}
